package me.notfirecodes.aquaman;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;

import java.util.Objects;

public final class Aquaman extends JavaPlugin implements Listener {

    @Override
    public void onEnable() {
        // Plugin startup logic
        getServer().getPluginManager().registerEvents(this, this);
        BukkitTask keepRainingTask = new KeepRainingTask(this).runTaskTimer(this, 0L, 100L);
        // This line is useless but IntelliJ was getting mad so I did this
        keepRainingTask.isCancelled();
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }

    // Function to give players the aquaman items
    public void giveItems(Player player) {
        ItemStack tridentItemStack = new ItemStack(Material.TRIDENT, 1);
        tridentItemStack.addEnchantment(Enchantment.RIPTIDE, 3);
        ItemMeta tridentItemMeta = tridentItemStack.getItemMeta();
        Objects.requireNonNull(tridentItemMeta).setDisplayName(ChatColor.AQUA + "Aquaman's Trident");
        tridentItemMeta.setUnbreakable(true);
        tridentItemStack.setItemMeta(tridentItemMeta);
        player.getInventory().clear();
        player.getInventory().addItem(tridentItemStack);
        ItemStack elytraItemStack = new ItemStack(Material.ELYTRA, 1);
        elytraItemStack.addUnsafeEnchantment(Enchantment.ARROW_INFINITE, 10);
        ItemMeta elytraItemMeta = elytraItemStack.getItemMeta();
        Objects.requireNonNull(elytraItemMeta).setDisplayName(ChatColor.AQUA + "Aquaman's Zero-Gravity Device");
        elytraItemMeta.setUnbreakable(true);
        elytraItemStack.setItemMeta(elytraItemMeta);
        player.getInventory().setChestplate(elytraItemStack);
    }

    // Gives players the aquaman items when they respawn
    @EventHandler
    public void onPlayerRespawnEvent(PlayerRespawnEvent event) {
        Player player = event.getPlayer();
        giveItems(player);
    }

    // Gives players the aquaman items when they join
    @EventHandler
    public void onPlayerJoinEvent(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        giveItems(player);
        player.sendMessage(ChatColor.BLUE + "[Aquaman]" + ChatColor.AQUA + " You're not Aquaman, you just have a pointy fork!");
    }

    // Clears players' inventories when they die
    @EventHandler
    public void onPlayerDeathEvent(PlayerDeathEvent event) {
        Player player = event.getEntity();
        event.getDrops().clear();
        event.setDeathMessage(ChatColor.AQUA + "[Aquaman] " + ChatColor.RED + player.getName() + ChatColor.AQUA + " was too mighty for their trident.");
    }

    // Stop players from dropping items
    @EventHandler
    public void onPlayerDropItemEvent(PlayerDropItemEvent event) {
        event.setCancelled(true);
    }
}
